﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;

namespace nwrole.toolbox.sync.entity
{

    public class nhEntityView 
    {
        public virtual long Id { get; set; }

        public virtual string Name { get; set; }

        public virtual string Model { get; set; }

        public virtual IList<nhEntityViewColumn> Columns { get; set; }

        public virtual string Content
        {
            get
            {
                return GridComposer.GenerateAspx(this, "mynamespace.Instrument");
            }
        }
    }

    public class nhEntityViewMapping : ClassMap<nhEntityView>
    {
        public nhEntityViewMapping()
        {
            Id(x => x.Id);
            Map(x => x.Name).Length(32).Not.Nullable();
            Map(x => x.Model);
            HasMany(x => x.Columns).KeyColumns.Add("nhEntityViewId").Inverse().Cascade.All();
        }
    }


    public class GridComposer
    {

        #region aspx
        public static string GenerateAspx(nhEntityView view, string className)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<%@ Page Language=\"C#\" Inherits=\"System.Web.Mvc.ViewPage<IEnumerable<{0}>>\" %>\r\n", className);

            sb.AppendLine("var connectionString = \"Data Source=146.66.234.53;Initial Catalog=nwrole;User Id=nwrole_user;Password=nwrole123!_\";");
            sb.AppendLine("var rep = new mynamespace.MyRepository();");
            sb.AppendLine("var data = rep.LoadList(rep.CreateSessionFactory(connectionString));");

            sb.AppendFormat("<% Html.Telerik().Grid({0})\r\n", view.Model);
            sb.AppendFormat(".Name(\"{0}\")\r\n", view.Name);
            if (view.Columns.Any())
            {
                sb.Append(".Columns(columns => {\r\n");
                foreach (var column in view.Columns)
                {
                    if (column.BindingType==nhEntityViewColumn.BindingTypeEnum.Bound)
                    {
                        sb.Append(string.Format("columns.Bound(k => k.{0})\r\n", column.Name));
                        if (!string.IsNullOrEmpty(column.Title))
                        {
                            sb.Append(string.Format(".Title(\"{0}\")\r\n", column.Title));
                        }
                        if (!string.IsNullOrEmpty(column.HtmlAttributes))
                        {
                            sb.Append(".HtmlAttributes(new { {0} })\r\n".Replace("{0}", column.HtmlAttributes));
                        }
                        sb.Append(";\r\n");
                    }
                    else if (column.BindingType==nhEntityViewColumn.BindingTypeEnum.Template)
                    {
                        sb.Append(string.Format("columns.Template({0})\r\n", column.Template));
                        if (!string.IsNullOrEmpty(column.Title))
                        {
                            sb.Append(string.Format(".Title(\"{0}\")\r\n", column.Title));
                        }
                        if (!string.IsNullOrEmpty(column.HtmlAttributes))
                        {
                            sb.Append(".HtmlAttributes(new { {0} })\r\n".Replace("{0}", column.HtmlAttributes));
                        }
                        sb.Append(";\r\n");
                    }
                }
                sb.Append("})\r\n");
                sb.Append(".Render(); %>\r\n");

            }

            return sb.ToString();
        }
        #endregion
                
    }
}

